package gui_layer;

import java.awt.Point;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import model_layer.Restock;
import ctr_layer.RestockCtr;

@SuppressWarnings("serial")
public class ManageRestockGUI extends SaleRestockManageWindow
{
	protected String[] column_names = { "id", "Date", "Supplier Name", "Total Price" };
	private RestockCtr res_ctr;
	
	public ManageRestockGUI()
	{
		super();
		res_ctr = new RestockCtr();
		setTitle("Manage Restocks");
		setBounds(100, 100, 450, 300);
		prepare_table(new Object[0][4], column_names);
		
		fill_table();
		
		this.addInternalFrameListener(new InternalFrameAdapter()
		{		
			@Override
			public void internalFrameActivated(InternalFrameEvent arg0)
			{
				refresh_table();
			}
		});
	}
	
	private void refresh_table()
	{
		MyTableModel mtm = (MyTableModel) table.getModel();
		mtm.setDataVector(new Object[0][4], column_names);
		fill_table();
		table.removeColumn(table.getColumnModel().getColumn(0));
		table.repaint();
	}
	
	private void fill_table()
	{
		List<Restock> restocks = res_ctr.find_all_restocks();
		Object[] data;
		MyTableModel mtm = (MyTableModel) table.getModel();
		for(Restock restock : restocks)
		{
			data = new Object[5];
			data[0] = restock.getId();
			data[1] = GUIUtilities.convert_date_to_string(restock.getReceivement_date());
			data[2] = restock.getSupplier().getName();
			data[3] = restock.getTotal_price();
			mtm.addRow(data);
		}
	}

	@Override
	protected void create_new()
	{
		NewRestockGUI nr_gui = new NewRestockGUI(null);
		
		this.getDesktopPane().add(nr_gui);
		Point window_corner = this.getLocation();
		nr_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
		nr_gui.setVisible(true);
		nr_gui.addInternalFrameListener(new InternalFrameAdapter()
		{		
			@Override
			public void internalFrameClosing(InternalFrameEvent arg0)
			{
				refresh_table();
			}
		});
	}

	@Override
	protected void details()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			Restock restock = res_ctr.find_restock((int) table.getModel().getValueAt(row, 0));
			if(restock != null)
			{
				NewRestockGUI nr_qui = new NewRestockGUI(restock);
				this.getDesktopPane().add(nr_qui);
				Point window_corner = this.getLocation();
				nr_qui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
				nr_qui.setVisible(true);
			}
			else
			{
				refresh_table();
			}
		}
	}

	@Override
	protected void delete()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to delete this restock? \n"
						+ "This action cannot be undone.", "Delete Sale", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(res_ctr.delete_restock((int) table.getModel().getValueAt(row, 0)))
				{
					JOptionPane.showMessageDialog(this, "Restock successfully deleted!", "Success", JOptionPane.PLAIN_MESSAGE);
				}
				else
				{
					JOptionPane.showMessageDialog(this, "Restock was not deleted!", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
			refresh_table();
		}
	}
}
